<?php

/**
 * This is the model class for table "tbl_ad".
 *
 * The followings are the available columns in table 'tbl_ad':
 * @property integer $id
 * @property string $img_url
 * @property string $img_title
 * @property string $ad_url
 * @property integer $type_id
 * @property string $create_date
 * @property string $update_date
 */
class Ad extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Ad the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_ad';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
        return array(
            array('img_url', 'required','message'=>'图片地址不能为空'),
            array('img_url', 'url','message'=>'图片地址格式错误'),
            array('img_title', 'required','message'=>'图片标题不能为空'),
            array('ad_title', 'required','message'=>'广告标题不能为空'),
            array('ad_url', 'required','message'=>'广告地址不能为空'),
            array('ad_url', 'url','message'=>'广告地址格式错误'),
            array('type_id', 'required','message'=>'广告类型不能为空'),
			array('type_id', 'numerical', 'integerOnly'=>true),
			array('img_url, img_title, ad_url', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('img_url, img_title, ad_url, type_id, create_date, update_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
        return array(
            'type'=>array(self::BELONGS_TO, 'Type', 'type_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'img_url' => '图片地址',
            'img_title' => '图片标题',
            'ad_title' => '广告标题',
			'ad_url' => '广告地址',
			'type_id' => '广告类型',
			'create_date' => 'Create Date',
			'update_date' => 'Update Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('img_url',$this->img_url,true);
        $criteria->compare('img_title',$this->img_title,true);
        $criteria->compare('ad_title',$this->ad_title,true);
		$criteria->compare('ad_url',$this->ad_url,true);
		$criteria->compare('type_id',$this->type_id);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('update_date',$this->update_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
    }


    /**
	 * 行为操作
	 */
	public function behaviors(){
		return array(
				'CTimestampBehavior' => array(
						'class' => 'zii.behaviors.CTimestampBehavior',
						'createAttribute' => 'create_date',
						'updateAttribute' => 'update_date',
						'setUpdateOnCreate' => 'true',
				)

		);
	}   
}
